package com.ujiuye.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ujiuye.model.Page;
import com.ujiuye.model.Student;
import com.ujiuye.service.StudentService;
import com.ujiuye.service.impl.StudentServiceImpl;
import com.ujiuye.util.DateUtils;
import javafx.geometry.Side;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.UUID;

@MultipartConfig
@WebServlet("*.do")
public class StudentServlet extends BaseServlet {
    //add 添加学生信息
    public void add(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {

        //普通参数的获取
        String stuName = req.getParameter("stuName");
        String stuGender = req.getParameter("stuGender");
        String stuBir = req.getParameter("stuBir");
        String[] stuHobby = req.getParameterValues("stuHobby");

        //获取上传的文件
        Part part = req.getPart("file");
        //获取上传的文件名称
        String photos = part.getSubmittedFileName();
        //获取上传文件的类型
        String suffix = photos.substring(photos.lastIndexOf("."));
        //为了解决文件名重复的问题,使用UUID重新生成文件名称
        String filename = UUID.randomUUID().toString() + suffix;
        //将图片保存到指定的磁盘
        String path = "D:\\系统班\\img";
        String absolutePath = path + File.separator + filename;
        //将文件写入指定的磁盘,其中参数为磁盘的额绝对路径
        part.write(absolutePath);
        //数据的封装

        Student student = new Student(
                stuName,
                stuGender,
                DateUtils.strTransformerDate(stuBir,"yyyy-MM-dd"),
                Arrays.toString(stuHobby),
                filename);
        //调用业务逻辑层方法
        StudentService ss = new StudentServiceImpl();

        boolean b = ss.saveStudent(student);
        //控制页面的跳转
        if (b) {
            resp.sendRedirect("queryStu.do");
        } else {
            req.getRequestDispatcher("add.jsp").forward(req, resp);
        }
    }

    // 学生信息的查询
    //学生信息的分页显示
    public void queryStu(HttpServletRequest req, HttpServletResponse resp) throws ServletException,IOException {
        //1. 获取参数
        String currentPage = req.getParameter("currentPage");
        String stuName = req.getParameter("stuName");
        String stuGender = req.getParameter("stuGender");

        //2.数据封装
        Student student = new Student(stuName, stuGender);
        //3.调用业务逻辑层方法
        //调用业务逻辑层方法
        StudentService ss = new StudentServiceImpl();
        Page<Student> page = ss.stuPageLists(currentPage, student);
        //4.页面跳转
       // 将java对象转变成json格式字符串
        ObjectMapper om = new ObjectMapper();
        String json = om.writeValueAsString(page);
        System.out.println("json="+json);
        // 在将其写给浏览器
        resp.getWriter().print(json);
    }

    //根据学生编号批量删除学生信息
    public void deleteStudentById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String ids = req.getParameter("ids");
        StudentService ss = new StudentServiceImpl();
        int num = ss.deleteStudentByIds(ids);
        System.out.println(num);
        resp.sendRedirect("queryStu.do");
    }
    // 根据学生编号获取学生信息并回显
    public void updateById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String sid = req.getParameter("sid");
        StudentService ss = new StudentServiceImpl();
        Student student = ss.updateStudentByIds(sid);
        //将数据存放到作用域中
        req.setAttribute("student",student);
        req.getRequestDispatcher("page/updateStu.jsp").forward(req,resp);
    }
    //完成学生信息的修改
    public void updateStudent(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //普通参数的获取
        String sid = req.getParameter("sid");
        String stuName = req.getParameter("stuName");
        String stuGender = req.getParameter("stuGender");
        String stuBir = req.getParameter("stuBir");
        String[] stuHobby = req.getParameterValues("stuHobby");

        String oldphoto = req.getParameter("photo");

        Part file = req.getPart("file");
        String photos = file.getSubmittedFileName();   //如果没有文件上传
        if(!"".equals(photos)){
            //获取上传文件的类型
            String suffix = photos.substring(photos.lastIndexOf("."));
            //为了解决文件名重复的问题,使用UUID重新生成文件名称
            // 如果有文件上传,就覆盖掉就的
            oldphoto = UUID.randomUUID().toString() + suffix;

            //将图片保存到指定的磁盘
            String path = "D:\\系统班\\img";
            String absolutePath = path + File.separator + oldphoto;
            //将文件写入指定的磁盘,其中参数为磁盘的额绝对路径
            file.write(absolutePath);
        }

        Student student = new Student(
                stuName,
                stuGender,
                DateUtils.strTransformerDate(stuBir,"yyyy-MM-dd"),
                Arrays.toString(stuHobby),
                oldphoto);
        student.setSid(sid);
        StudentService ss = new StudentServiceImpl();
        boolean bo =  ss.updateStudentDo(student);
        // 跳转到查询学生的页面
        resp.sendRedirect("queryStu.do");
    }
}